#include "widget_conversation.h"
#include "ui_widget_conversation.h"
#include <QScrollBar>

widget_conversation::widget_conversation(QWidget *parent,
                                         QString startingConversationText,
                                         QString startingInputText) :
    QWidget(parent),
    ui(new Ui::widget_conversation)
{
    this->firstTextEntered = false;
    ui->setupUi(this);
    if(!startingConversationText.isEmpty()){
        ui->chatWindow->setText(startingConversationText);
    }
    if(!startingInputText.isEmpty()){
        ui->textEnter->setPlaceholderText(startingInputText);
    } else {
        QString text = ui->textEnter->text();
        ui->textEnter->clear();
        ui->textEnter->setPlaceholderText(text);
    }
}

widget_conversation::~widget_conversation()
{
    delete ui;
}

void widget_conversation::on_SubmitButton_clicked()
{
    if (ui->textEnter->text().length() != 0)
    {
        ui->chatWindow->setText(ui->chatWindow->toPlainText() + '\n' + "You: " + ui->textEnter->text());
        QScrollBar* scroll = ui->chatWindow->verticalScrollBar();
        scroll->setValue(scroll->maximum());
        ui->textEnter->clear();
    }
    ui->textEnter->setFocus();
}

void widget_conversation::on_textEnter_returnPressed()
{
    on_SubmitButton_clicked();
}
